package com.example.test.test;

import java.util.BitSet;

public class Sieve {
    public static void main(String[] args) {
        int n = 10;
        long start = System.currentTimeMillis();
        BitSet bitSet = new BitSet(n + 1);
        int count = 0;
        int i;
        for (i = 2 ;i<=n;i++)
            bitSet.set(i);
            i = 2;
            while (i * i <=n){
                if(bitSet.get(i)){
                    count++;
                    int k = 2 * i;
                    while (k<=n){
                        bitSet.clear(k);
                        k+= i;
                    }
                }
                i++;
            }
        while (i<=n){
            if (bitSet.get(i)) count++;
            i++;
        }
        long end =System.currentTimeMillis();
        System.out.println("素数个数="+count);
        System.out.println("耗时="+(end-start));
    }
}
